//
//  RegistSetupFour.m
//  HB
//
//  Created by 孙智慧 on 16/1/27.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "RegistSetupFour.h"

#import "LoginOperation.h"

@implementation RegistSetupFour

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self->sex=0;
    
    self.txtName.text=self.uName;
    
    self.wmBtn.layer.cornerRadius = 2.0;
    
    self.mBtn.layer.cornerRadius=2.0;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //显示导航栏
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
    
    [self setNavigationRight:@"完成" sel:@selector(nextSteup)];
    
}

-(void) nextSteup
{
    [self waitingFor:@"正在保存数据" andMethod:@selector(myTask) onTarget:self];
}

- (void)myTask
{
    
    operation=[[NetWorkOperation alloc] initWithDelegate:self];
    //开始保存数据
    
    NSString *lat=[[NSUserDefaults standardUserDefaults] objectForKey:@"CCLastLatitude"];
    
    NSString *lon=[[NSUserDefaults standardUserDefaults] objectForKey:@"CCLastLongitude"];
    
    NSString *city=[[NSUserDefaults standardUserDefaults] objectForKey:@"CCLastCity"];
    
    [BaseLoginController setSex:self->sex];
    
    NSString *mobile= [BaseLoginController getMobile];
    
    NSString *uname= [BaseLoginController getUName];
    
    NSString *upwd = [BaseLoginController getPwd];
    
    NSString *ssex= [NSString stringWithFormat:@"%ld",(long)self->sex];
    
    NSString *url= [FxGlobal getServiceUrl:@"user" andFuncName:@"Register"];
    
    NSString *md5Str= [NSString stringWithFormat: @"city=%@&lat=%@&lon=%@&mobile=%@&sex=%@&uname=%@&upwd=%@&key=tqa",city,lat,lon,mobile,ssex,uname,upwd];
    
    NSString *sign = [Common createMD5:md5Str];
    
    NSString *body=[NSString stringWithFormat:@"{\"city\":\"%@\",\"lat\":\"%@\",\"lon\":\"%@\",\"RegFrom\":\"HB\",\"mobile\":\"%@\",\"uname\":\"%@\",\"upwd\":\"%@\",\"sex\":\"%@\",\"sign\":\"%@\",\"clicentid\":\"1\"}",city,lat,lon,mobile,uname,upwd,ssex,sign];
    
    NSDictionary *dictInfo = @{@"url":url,@"body":body};
    
    [operation setOpInfo:dictInfo];
    
    [operation post];
    
}


-(void) opSuccessForPost:(id)data
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        
        operation=[[NetWorkOperation alloc] initWithDelegate:self];
        
        NSString *uName= [BaseLoginController getUName];
        
        NSString *uPass= [BaseLoginController getPwd];
        
        NSString *urlS= [FxGlobal getServiceUrl:@"user" andFuncName:@"login"];
        
        NSString *url= [NSString stringWithFormat:@"%@?uName=%@&upwd=%@",urlS,uName,uPass];
        
        NSDictionary *dictInfo = @{@"url":url};
        
        [operation setOpInfo:dictInfo];
        
        [operation get:0];
        
    }
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation= nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSString *djID=[data1 objectForKey:@"djID"];
            
            [Common setUserDefaults:@"djID" andValue:djID];
            
            UserInfo *ui=[[UserInfo alloc]init];
            
            ui.djID=[data1 objectForKey:@"djID"];
            
            ui.nickName=[data1 objectForKey:@"UserName"];
            
            ui.headImgUrl=[data1 objectForKey:@"HeadImg"];
            
            ui.headImgName =[data1 objectForKey:@"HeadImgName"];
            
            ui.login = YES;
            
            NSDate *date=[NSDate date];
            
            ui.loginDate= date ;
            
            NSString *code=[data1 objectForKey:@"Code"];
            
            ui.code=[code intValue];
            
            NSString *vipID=[data1 objectForKey:@"VipID"];
            
            ui.VipID = [vipID integerValue];
            
            ui.VipExpiredTime= [Common convertDateFromString:[data1 objectForKey:@"VipExpiredTime"]];
            
            ui.QualityID= [[data1 objectForKey:@"QualityID"] intValue];
            
            DBHelper  *db=[DBHelper sharedMyClass];
            //保存 用户信息
            [db addData:ui];
            
            //登陆成功后显示主页
            [self hideLanch:YES];
        });
    }
    else
    {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //[Common showMsg:@"账户或密码错误。请重新登陆" andCancel:YES andHost:self];
        });
        
    }
    
}

- (IBAction)mClick:(id)sender
{
    if( ((UIButton *) sender).tag==0)
    {
        ((UIButton *) sender).backgroundColor= [UIColor redColor];
        
        ((UIButton *) sender).tag=1;
        
        self.wmBtn .backgroundColor= [UIColor whiteColor];
        
        self.wmBtn.tag=0;
        
        self->sex=1;
    }
    
}

- (IBAction)wmClick:(id)sender
{
    if( ((UIButton *) sender).tag==0)
    {
        ((UIButton *) sender).backgroundColor= [UIColor redColor];
        
        ((UIButton *) sender).tag=1;
        
        self.mBtn.backgroundColor=[UIColor whiteColor];
        
        self.mBtn.tag=0;
        
        self->sex=0;
    }
}

@end
